package server.servlet;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import server.xml.Person;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 熟悉sax的流程
 */
public class XmlTest{
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        //SAX解析
        //1、获取解析工厂
        SAXParserFactory factory=SAXParserFactory.newInstance();
        //2、从解析工厂获取解析器
        SAXParser parse =factory.newSAXParser();
        //3、编写处理器
        //4、加载文档 Document 注册处理器
        webHandler handler=new webHandler();
        //5、解析
        parse.parse(Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream("server/servlet/web.xml")
                ,handler);

        //获取数据
        WebContext context = new WebContext(handler.getEntitys(),handler.getMappings());
        //假设请求的是/login
        String className = context.GetClz("/g");
       //反射
        Class clz = Class.forName(className);
        Servlet servlet = (Servlet)clz.newInstance();
        System.out.println(servlet);
        servlet.service();
    }
}

class webHandler extends DefaultHandler{
    private List<Entity> entitys;
    private List<Mapping> mappings;
    private Entity entity;
    private Mapping mapping;
    private String tag;//存储操作的标签
    private boolean isMapping = false;

    public List<Entity> getEntitys() {
        return entitys;
    }

    public List<Mapping> getMappings() {
        return mappings;
    }


    @Override
    public void startDocument() throws SAXException {
        entitys = new ArrayList<Entity>();
        mappings = new ArrayList<Mapping>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if(null!=qName){
                tag = qName;//存储标签名
                if(qName.equals("servlet")){
                    entity = new Entity();
                    isMapping = false;
                }else if(qName.equals("servlet-mapping")){
                    mapping = new Mapping();
                    isMapping = true;
                }
            }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String contents = new String(ch,start,length).trim();
        if(null!=tag) {//处理空问题
            if (isMapping){//操作servlet-mapping
                if (tag.equals("servlet-name")) {
                    mapping.setName(contents);
                } else if (tag.equals("url-pattern")) {
                   mapping.addPattern(contents);
                }
            }else{//操作servlet
                if (tag.equals("servlet-name")) {
                    entity.setName(contents);
                } else if (tag.equals("servlet-class")) {
                   entity.setClz(contents);
                }
            }

        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(null!=qName) {//处理空问题
            if (qName.equals("servlet")) {
                entitys.add(entity);
            }else if (qName.equals("servlet-mapping")) {
                mappings.add(mapping);
            }
        }
        tag=null;
    }

}
